home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 684 / 684.xpi / chrome / fireftp.jar / content / js / etc / misc.js < prev    next >
Encoding:
Text File  |  2009-06-15  |  8.7 KB  |  276 lines

  1. function commas(num) {                 // add commas to numbers
  2.   num = num.toString();
  3.  
  4.   if (num.search(/\d{4}$/) == -1 || !gStrbundle.getString("numSep")) {
  5.     return num;
  6.   }
  7.  
  8.   num = num.replace(/\d{3}$/, gStrbundle.getString("numSep") + "$&");
  9.  
  10.   var a = new RegExp("\\d{4}" + (gStrbundle.getString("numSep") == "." ? "\\." : gStrbundle.getString("numSep")));
  11.   var b = new RegExp("\\d{3}" + (gStrbundle.getString("numSep") == "." ? "\\." : gStrbundle.getString("numSep")));
  12.  
  13.   while (num.search(a) != -1) {
  14.     num = num.replace(b, gStrbundle.getString("numSep") + "$&");
  15.   }
  16.  
  17.   return num;
  18. }
  19.  
  20. function zeros(num) {                  // pad with zeros
  21.   num = num.toString();
  22.   return num.length == 2 ? num : '0' + num;
  23. }
  24.  
  25. function onChangeType() {              // change TYPE A/TYPE I/auto from statusbar
  26.   gPrefs.setIntPref("filemode", (gFtp.fileMode + 1) % 3);
  27. }
  28.  
  29. function setInterfaceMode() {          // update the interface based on collapsing
  30.   gPrefs.setIntPref("interfacemode", ($('leftsplitter').getAttribute('state')  == 'collapsed') * 2
  31.                                    + ($('rightsplitter').getAttribute('state') == 'collapsed'));
  32. }
  33.  
  34. function updateInterface() {           // update the interface based on interfacemode variable
  35.   var local  = (gInterfaceMode & 2);
  36.   var remote = (gInterfaceMode & 1);
  37.  
  38.   $('storbutton').collapsed  = local;
  39.   $('retrbutton').collapsed  = remote;
  40.  
  41.   $('localview').collapsed   = local;
  42.   $('remoteview').collapsed  = remote;
  43.  
  44.   $('leftsplitter').setAttribute( 'state', (local  ? 'collapsed' : 'open'));
  45.   $('rightsplitter').setAttribute('state', (remote ? 'collapsed' : 'open'));
  46. }
  47.  
  48. function updateOpenMode() {
  49.   $('localUpload').setAttribute(   "defaultAction", gOpenMode == 0);
  50.   $('localOpen').setAttribute(     "defaultAction", gOpenMode == 1);
  51.   $('remoteDownload').setAttribute("defaultAction", gOpenMode == 0);
  52.   $('remoteOpen').setAttribute(    "defaultAction", gOpenMode == 1);
  53.  
  54.   $('localUpload').setAttribute(   "key", gOpenMode == 0 ? "key_transfer" : "");
  55.   $('localOpen').setAttribute(     "key", gOpenMode == 1 ? "key_transfer" : "key_open");
  56.   $('remoteDownload').setAttribute("key", gOpenMode == 0 ? "key_transfer" : "");
  57.   $('remoteOpen').setAttribute(    "key", gOpenMode == 1 ? "key_transfer" : "key_open");
  58. }
  59.  
  60. function onLocalPathFocus(event) {
  61.   gLocalPathFocus  = gLocalPath.value;
  62. }
  63.  
  64. function onLocalPathBlur(event) {
  65.   gLocalPath.value = gLocalPathFocus;
  66. }
  67.  
  68. function onRemotePathFocus(event) {
  69.   gRemotePathFocus = gRemotePath.value;
  70. }
  71.  
  72. function onRemotePathBlur(event) {
  73.   if (!gFtp.isConnected) {
  74.     gRemotePathFocus  = gRemotePath.value;
  75.   } else {
  76.     gRemotePath.value = gRemotePathFocus;
  77.   }
  78. }
  79.  
  80. function browseLocal(title) {          // browse the local file structure
  81.   var nsIFilePicker = Components.interfaces.nsIFilePicker;
  82.   var fp            = Components.classes["@mozilla.org/filepicker;1"].createInstance(nsIFilePicker);
  83.   fp.init(window, title ? title : gStrbundle.getString("selectFolder"), nsIFilePicker.modeGetFolder);
  84.   var res = fp.show();
  85.  
  86.   if (res == nsIFilePicker.returnOK) {
  87.     localDirTree.changeDir(fp.file.path);
  88.   }
  89.  
  90.   return res == nsIFilePicker.returnOK;
  91. }
  92.  
  93. function parseSize(size) {             // adds byte information for file sizes
  94.   if (size >= 1024 * 1024 * 1024) {
  95.     size = gStrbundle.getFormattedString("gigabyte", [parseFloat(size / 1024 / 1024 / 1024).toFixed(1).replace(/\./g, gStrbundle.getString("decimal"))]);
  96.   } else if (size >= 1024 * 1024) {
  97.     size = gStrbundle.getFormattedString("megabyte", [parseFloat(size / 1024 / 1024).toFixed(2).replace(/\./g, gStrbundle.getString("decimal"))]);
  98.   } else if (size >= 1024) {
  99.     size = gStrbundle.getFormattedString("kilobyte", [parseFloat(size / 1024).toFixed(1).replace(/\./g, gStrbundle.getString("decimal"))]);
  100.   } else if (size >= 0) {
  101.     size = gStrbundle.getFormattedString("bytes", [size]);
  102.   }
  103.  
  104.   return size;
  105. }
  106.  
  107. function displayWelcomeMessage(msg) {
  108.   if (gWelcomeMode) {
  109.     try {
  110.       if (gWelcomeWindow && gWelcomeWindow.close) {          // get rid of those extra pestering welcome windows if the program is reconnecting automatically
  111.         gWelcomeWindow.close();
  112.       }
  113.     } catch (ex) { }
  114.  
  115.     gWelcomeWindow = window.openDialog("chrome://fireftp/content/welcome.xul", "welcome", "chrome,resizable,centerscreen", msg);
  116.   }
  117. }
  118.  
  119. function custom() {
  120.   if (!gFtp.isConnected || !isReady()) {
  121.     return;
  122.   }
  123.  
  124.   var cmd = window.prompt(gStrbundle.getString("command"), "", gStrbundle.getString("customCommand"));
  125.  
  126.   if (!cmd) {
  127.     return;
  128.   }
  129.  
  130.   gFtp.custom(cmd);
  131. }
  132.  
  133. function cloneObject(what) {
  134.   for (i in what) {
  135.     this[i] = what[i];
  136.   }
  137. }
  138.  
  139. function cloneArray(a) {
  140.   var n = new Array();
  141.  
  142.   for (var x = 0; x < a.length; ++x) {
  143.     n.push(a[x]);
  144.   }
  145.  
  146.   return n;
  147. }
  148.  
  149. function runInFirefox(path) {
  150.   var windowManager          = Components.classes['@mozilla.org/appshell/window-mediator;1'].getService();
  151.   var windowManagerInterface = windowManager.QueryInterface(Components.interfaces.nsIWindowMediator);
  152.   var win                    = windowManagerInterface.getMostRecentWindow("navigator:browser");
  153.  
  154.   if (win) {
  155.     var theTab               = win.gBrowser.addTab(path);
  156.     win.gBrowser.selectedTab = theTab;
  157.     return;
  158.   }
  159.  
  160.   try {    // this is used if FireFTP is running as a standalone and there are no browsers open; much more complicated, not very pretty
  161.     var firefoxInstallPath = Components.classes["@mozilla.org/file/directory_service;1"].createInstance(Components.interfaces.nsIProperties)
  162.                                        .get("CurProcD", Components.interfaces.nsILocalFile);
  163.     var firefox            = localFile.init(firefoxInstallPath.path + "\\" + "firefox.exe");
  164.  
  165.     if (!firefox.exists()) {                                 // try linux
  166.       firefox.initWithPath(firefoxInstallPath.path + "/" + "firefox");
  167.       if (!firefox.exists()) {                               // try os x
  168.          firefox.initWithPath(firefoxInstallPath.path + "/" + "firefox-bin");
  169.       }
  170.     }
  171.  
  172.     var process = Components.classes['@mozilla.org/process/util;1'].createInstance(Components.interfaces.nsIProcess);
  173.     process.init(firefox);
  174.     var arguments = new Array(path);
  175.     process.run(false, arguments, arguments.length, {});
  176.   } catch (ex) {
  177.     debug(ex);
  178.   }
  179. }
  180.  
  181. function tipJar() {
  182.   if (!gDonated) {
  183.     gPrefs.setBoolPref("donated", true);
  184.     runInFirefox("http://fireftp.mozdev.org/donate.html");
  185.   }
  186. }
  187.  
  188. function doAbort() {
  189.   gSearchRunning = false;
  190.   var forceKill  = false;
  191.  
  192.   if (gFxp && gFxp.isConnected) {
  193.     gFxp.disconnect();
  194.     forceKill = true;
  195.   }
  196.  
  197.   for (var x = 0; x < gMaxCon; ++x) {
  198.     gConnections[x].abort(forceKill);
  199.     forceKill = false;
  200.   }
  201.  
  202.   queueTree.failed = new Array();
  203. }
  204.  
  205. function toolsPopupMenu() {
  206.   $('diffMenuItem').setAttribute("disabled",     !gFtp.isConnected || localTree.searchMode == 2 || remoteTree.searchMode == 2);
  207.   $('recDiffMenuItem').setAttribute("disabled",  !gFtp.isConnected || localTree.searchMode == 2 || remoteTree.searchMode == 2);
  208. }
  209.  
  210. function setCharAt(str, index, ch) {                         // how annoying
  211.   return str.substr(0, index) + ch + str.substr(index + 1);
  212. }
  213.  
  214. // thanks to David Huynh
  215. // http://mozilla-firefox-extension-dev.blogspot.com/2004/11/passing-objects-between-javascript.html
  216. function wrapperClass(obj) {
  217.   this.wrappedJSObject = this;
  218.   this.obj             = obj;
  219. }
  220.  
  221. wrapperClass.prototype = {
  222.   QueryInterface : function(iid) {
  223.     if (iid.equals(Components.interfaces.nsISupports)) {
  224.       return this;
  225.     }
  226.  
  227.     throw Components.results.NS_ERROR_NO_INTERFACE;
  228.   }
  229. }
  230.  
  231. function getPlatform() {
  232.     var platform = navigator.platform.toLowerCase();
  233.     
  234.     if (platform.indexOf('linux') != -1) {
  235.         return 'linux';
  236.     }
  237.  
  238.     if (platform.indexOf('mac') != -1) {
  239.         return 'mac';
  240.     }
  241.  
  242.     return 'windows';
  243. }
  244.  
  245. function doResizeReverseHack() {        // bah, humbug
  246.   $('localdirtree').setAttribute(    'flex', '1');
  247.   $('localdirtree').removeAttribute( 'width');
  248.   $('remotedirtree').setAttribute(   'flex', '1');
  249.   $('remotedirtree').removeAttribute('width');
  250.   $('localview').setAttribute(       'flex', '1');
  251.   $('localview').removeAttribute(    'width');
  252.   $('remoteview').setAttribute(      'flex', '1');
  253.   $('remoteview').removeAttribute(   'width');
  254.  
  255.   doResizeHack();
  256. }
  257.  
  258. function doResizeHack() {
  259.   $('localdirtree').setAttribute(    'width', $('localdirtree').treeBoxObject.width);
  260.   $('localdirtree').removeAttribute( 'flex');
  261.   $('remotedirtree').setAttribute(   'width', $('remotedirtree').treeBoxObject.width);
  262.   $('remotedirtree').removeAttribute('flex');
  263.   $('localview').setAttribute(       'width', $('localview').boxObject.width);
  264.   $('localview').removeAttribute(    'flex');
  265.   $('remoteview').setAttribute(      'width', $('remoteview').boxObject.width);
  266.   $('remoteview').removeAttribute(   'flex');
  267. }
  268.  
  269. function testAccelKey(event) {
  270.   if (getPlatform() == 'mac') {
  271.     return event.metaKey;
  272.   }
  273.  
  274.   return event.ctrlKey;
  275. }
  276.